﻿using dz.loger.core;
using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.InvDoc;
using dz.util.core;
using Dz.WorkerService.Utils;
using System.Data;

namespace Dz.WorkerService.Service
{
    public class TransferinService : BaseU9Service
    {
        private string transInDocType, transInWh;
        public TransferinService(string processId):base(processId)
        {
            table = "dz_jky_SO";
            docType = "出货-调拨";
            transInDocType = Config.Instance.GetValue("TransInDocType");
            transInWh = Config.Instance.GetValue("TransInWh");
            controller = "TransferIn";
        }

        protected async override Task<DataTable> GetProcessData()
        {            
            string sql = "select distinct org,deptcode,shipdate,wh from dz_jky_SO where flag=1 and sellcount >0 and xtype=1";

            DataTable dt = await sqlUtils.Fill(sql);

            return dt;
        }

        protected override async Task Process()
        {
            foreach (DataRow dr in dt.Rows)
            {
                await Import(dr);
            }
        }

        private async Task Import(DataRow dr)
        {
            string sql = $@"select goodsno,sum(sellcount) as sellcount from dz_jky_SO 
                        where flag=1 and xtype=1 and sellcount > 0 and org = '{dr["org"]}' and wh = '{dr["wh"]}' 
                        and deptcode = '{dr["deptcode"]}' and shipdate = '{dr["shipdate"]}' 
                        group by goodsno ";

            DataTable result = await sqlUtils.Fill(sql);

            await Import(dr, result);
        }

        private async Task Import(DataRow dr,DataTable dt)
        {
            string org = dr["org"].ToString();
            string wh = dr["wh"].ToString();
            string dept = dr["deptcode"].ToString();
            
            string extWhere = $" and org = '{org}' and wh = '{wh}' and deptcode = '{dept}' and shipdate = '{dr["shipdate"]}'";

            DzTransferIn transferIn = new DzTransferIn();

            transferIn.Org = org;
            transferIn.TransInDocType = transInDocType;
            transferIn.BusinessDate = Convert.ToDateTime(dr["shipdate"]);

            List<DzTransInLine> lines = new List<DzTransInLine>();

            foreach (DataRow dr2 in dt.Rows) 
            {                
                string inv = dr2["goodsno"].ToString();

                if (!InvUtils.Instance.IsExists(inv, org))
                {
                    LogUtil.Instance.Debug($"存货{inv}不存在，跳过");
                    continue;
                }

                DzTransInLine line = new DzTransInLine();

                line.ItemInfo = inv;
                line.CostUOMQty = Convert.ToDecimal(dr2["sellcount"]);
                line.StoreUOMQty = line.CostUOMQty;
                line.TransInWh = transInWh;

                #region 货位
                List<DzTransInBin> bins = new List<DzTransInBin>();

                DzTransInBin bin = new DzTransInBin();
                
                bin.StoreUOMQty = line.StoreUOMQty;
                DzBinInfo binInfo = new DzBinInfo();
                binInfo.Code = dept;
                DzBin dzBin = new DzBin();
                dzBin.Code = dept;
                dzBin.Warehouse = transInWh;
                binInfo.Bin = dzBin;
                bin.BinInfo = binInfo;

                bins.Add(bin);

                line.TransInBins = bins;
                #endregion
                #region 明细子行
                List<DzTransInSubLine> subLines = new List<DzTransInSubLine>();

                DzTransInSubLine subLine = new DzTransInSubLine();

                subLine.TransOutWh = wh;
                subLine.TransOutOrg = org;

                subLines.Add(subLine);
                #endregion

                line.TransInSubLines = subLines;
                lines.Add(line);
            }

            transferIn.TransInLines = lines;

            //U9CResult result = await U9Utils.Post(controller, "AddAndAudit", JsonUtil.Serialize(transferIn));

            U9CResult result = await U9Utils.Post(controller, "Add", JsonUtil.Serialize(transferIn));

            await UpdateSyncLog(result, extWhere);

        }
    }
}
